import sys
from pathlib import Path
from os.path import dirname
path = dirname(str(Path(__file__).resolve().parents[2]))
if path not in sys.path:
    sys.path.append(path)
from xkw_data_query.db.db_caller import MysqlCaller

class PaperSplitDbCaller:
    """
    生产环境paper split数据库访问类
    """
    def __init__(self):
        self.ops_dba_caller = MysqlCaller("base-RBM-10.111.109.142-3307-查询", "paper_split")

    def _query_from_prod_db(self, sql_content):
        res = self.ops_dba_caller.query_from_db(sql_content)
        return res

    def get_papers_by_ids(self, paper_ids: list[int]):
        """
        paper_ids: list[int],是线上的paper_id
        """
        paper_ids_all_str = ",".join([str(paper_id) for paper_id in paper_ids])
        sql_content = f"""
        select id, app_id, source_id, course_id, oss_path, status, creator_id, create_time, update_time, split_model from paper where id in ({paper_ids_all_str})
        """
        res_1 = self._query_from_prod_db(sql_content)
        papers_all = []
        for row in res_1:
            paper = {
                "id": row[0],
                "app_id": row[1],
                "source_id": row[2],
                "course_id": row[3],
                "oss_path": row[4],
                "status": row[5],
                "creator_id": row[6],
                "create_time": row[7],
                "update_time": row[8],
                "split_model": row[9]
            }
            papers_all.append(paper)
        return papers_all
    
    def get_paragraphs_by_paper_id(self, paper_id: int):
        sql_content = f"""
        select id, paper_id, content, type, ordinal from paper_paragraph where paper_id = {paper_id}
        """
        res = self._query_from_prod_db(sql_content)
        paragraphs = []
        for row in res:
            paragraph = {
                "id": row[0],
                "paper_id": row[1],
                "content": row[2],
                "type": row[3],
                "ordinal": row[4]
            }
            paragraphs.append(paragraph)
        return paragraphs


